/********************************************************
**      prog4.c                                        **
**      Queue and Stack example assignment             **
**      Phuong Nguyen								   **
**		H422U466			                           **
**      Due 4/15/2013                                  **
********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prog4.h"

typedef struct node_type
{
    int data;
    struct node_type *next;
}NODE;

int main()
{
    int *x;
    QUEUE *Q1, *Q2;
    Q1 = CreateQueue();
    Q2 = CreateQueue();

    printf("Q1: %d  Q2: %d\n",Q1,Q2);


    _Push(Q1, 1);
    _Push(Q1, 2);
    _Push(Q1, 3);
    _Push(Q1, 4);
    _Push(Q1, 5);

    print_queue(Q1);

    _Pop(Q1,Q2,x);

    printf("Q1: %d  Q2: %d",Q1,Q2);

    //print_queue(Q1);
    //print_queue(Q2);
    return 0;
}

BOOLEAN _Push (QUEUE *Q1, int x)
{
    ELEMENT data;
    ELEMENT *tmp;
    tmp = &data;
    tmp->value = x;
    EnQueue(Q1, data);
}

BOOLEAN _Pop (QUEUE *Q1, QUEUE *Q2, int *x)
{
    QUEUE *temp_queue;

    ELEMENT data;
    ELEMENT *tmp;
    tmp = &data;
    tmp->value = x;

    if(EmptyQueue(Q1)) return FALSE;
    while(Q1->count>1)
    {
        DeQueue(Q1, tmp);
        EnQueue(Q2, data);
    }

    //printf("temp: %d",temp_queue);
    printf("POP Q1: %d  Q2: %d",Q1,Q2);

    temp_queue = Q1;
    Q1 = Q2;
    Q2 = temp_queue;

    //printf("temp: %d",temp_queue);
    printf("\nPOP Q1: %d  Q2: %d\n",Q1,Q2);

}


void menu()
{
    printf("==================Choose one of the following options==================");
    printf("\n1. Delete all the negative integers without changing the order of the remaining elements in a queue.");
    printf("\n2. Reverse the contents of a queue using a stack.");
    printf("\nYour choice: ");
}
void process()
{
    int choice=0;
    char temp[10];
    while((choice!=1) && (choice!=2))
    {
        menu();
        fgets(temp, 50, stdin);
        sscanf(temp, "%d", &choice);

        switch(choice)
        {
            case 1:
                delete_neg_int();
                break;
            case 2:
                reverse_queue();
                break;
        }
    }
}

QUEUE *user_input()
{
    QUEUE *a_queue;
    a_queue = CreateQueue();

    ELEMENT data;
    ELEMENT *temp_data;
    temp_data = &data;

    int input=0;
    char temp[10];
    while(input!=999)
    {
        printf("Enter a number: ");
        fgets(temp, 50, stdin);
        sscanf(temp, "%d", &input);
        if(input!=999)
        {
            data.value = input;
            EnQueue(a_queue, data);
        }
    }
    return a_queue;
}

void delete_neg_int()
{
    QUEUE *a_queue, *out_queue;
    a_queue = user_input();
    printf("\nOriginal Sequence: ");
    print_queue(a_queue);

    out_queue = CreateQueue();

    ELEMENT data;
    ELEMENT *temp_data;
    temp_data = &data;

    while(!EmptyQueue(a_queue))
    {
        DeQueue(a_queue, temp_data);
        if(temp_data->value>=0)
        {
            EnQueue(out_queue, data);
        }
    }
    printf("\nModified Sequence: ");
    print_queue(out_queue);
}


void reverse_queue()
{
    QUEUE *a_queue;
    a_queue = user_input();
    STACK *a_stack;
    a_stack = CreateStack();

    printf("\nOriginal Sequence: ");
    print_queue(a_queue);

    ELEMENT data;
    ELEMENT *temp_data;
    temp_data = &data;

    while(!EmptyQueue(a_queue))
    {
        DeQueue(a_queue, temp_data);
        Push(a_stack, data);
    }
    while(!EmptyStack(a_stack))
    {
        Pop(a_stack, temp_data);
        EnQueue(a_queue, data);
    }

    printf("\nModified Sequence: ");
    print_queue(a_queue);
}

void print_queue(QUEUE *a_queue)
{
    QUEUE *temp_queue;
    temp_queue = CreateQueue();

    ELEMENT data;
    ELEMENT *temp_data;
    temp_data = &data;

    while(!EmptyQueue(a_queue))
    {
        DeQueue(a_queue, temp_data);
        printf("%d ", temp_data->value);
        EnQueue(temp_queue, data);
    }
    while(!EmptyQueue(temp_queue))
    {
        DeQueue(temp_queue, temp_data);
        EnQueue(a_queue, data);
    }
    printf("\n");
}
